/**
 * Copyright 2018 Alibaba Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alibaba.android.bindingx.playground.weex.extension.lottie;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;

import com.airbnb.lottie.LottieAnimationView;
import com.alibaba.android.bindingx.core.BindingXPropertyInterceptor;
import com.alibaba.android.bindingx.core.PlatformManager;

import java.util.Map;

/**
 * Description:
 *
 * bindingx属性拦截器。
 * 使用bindingx控制lottie动画进度
 *
 * Created by rowandjj(chuyi)<br/>
 */
public class LottieProgressInterceptor implements BindingXPropertyInterceptor.IPropertyUpdateInterceptor {

    @Override
    public boolean updateView(@Nullable View targetView,
                              @NonNull String propertyName,
                              @NonNull Object propertyValue,
                              @NonNull PlatformManager.IDeviceResolutionTranslator translator,
                              @NonNull Map<String, Object> config,
                              Object... extension) {
        if(!"lottie-progress".equals(propertyName)) {
            return false;
        }

        if(targetView == null || !(targetView instanceof LottieAnimationView)) {
            return false;
        }
        double progress = (double) propertyValue;
        LottieAnimationView animationView = (LottieAnimationView) targetView;
        animationView.setProgress((float)progress);
        return true;
    }
}
